eval 'exec perl $0 ${1+"$@"}' # -*- perl -*-
if 0;

# BETA RELEASE 6

# Copyright (c) 2005, Shawn Boyette
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, provided
# that the above copyright notice(s) and this permission notice appear
# in all copies of the Software and that both the above copyright
# notice(s) and this permission notice appear in supporting
# documentation.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR
# ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY
# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
# OF THIS SOFTWARE.
#
# Except as contained in this notice, the name of a copyright holder
# shall not be used in advertising or otherwise to promote the sale,
# use or other dealings in this Software without prior written
# authorization of the copyright holder.

use warnings;
use strict;
use lib ".";

use Config::YAML;
use Curses::UI;
use DBI;
use XML::Simple;

use OliveEdit;
use OliveFeed;
use OliveOpts;
use OliveMisc;
use OliveStory;
use OliveWindow;

$XML::Simple::PREFERRED_PARSER = "XML::Parser";

my $rev = '$Rev$';
$rev =~ s/\D//g;
$rev = uc(sprintf("%4.4x",$rev));
my $tz  = `date +%z`;
$tz =~ s/00$//;

# create Curses::UI object
my $cui = new Curses::UI( -color_support => 1,
                          -clear_on_exit => 1,
                          -mouse_support => 0,
                        );

# system init; create windows and userdata storage
sysinit($cui);
my $root = $cui->add('root', 'Window');
my $ud = { dbh   => DBI->connect("dbi:SQLite:dbname=$ENV{HOME}/.olive/stories.db","",""),
           xml   => XML::Simple->new,
           feeds => "$ENV{HOME}/.olive/feeds",
           focus => 'news',
           rev   => $rev,
           opts  => [ qw(sls snu fos dst coe) ],
           tz    => $tz,
         };
$cui->userdata($ud);
$cui->userdata->{c}    = Config::YAML->new( config => "$ENV{HOME}/.olive/olive.yaml" ),
$cui->userdata->{wins} = wininit($cui,$root),

# set up database and keybindings
sysinit2($cui);
$cui->set_binding(sub { getfeed($cui) },         "\cA");
$cui->set_binding(sub { deletefeed($cui) },      "\cR");
$cui->set_binding(sub { editfeed($cui) },        "\cE");
$cui->set_binding(sub { setopts($cui) },         "\cO");
$cui->set_binding(sub { winredraw($cui,$root) }, "\cL");
$cui->set_binding(sub { aboutpop($cui) },        "\cT");
$root->set_binding(sub { followlink($cui) }, 'l');
$root->set_binding(sub { markone($cui,1) },  'm');
$root->set_binding(sub { markone($cui,0) },  'M');
$root->set_binding(sub { markall($cui,1) },  ',');
$root->set_binding(sub { markall($cui,0) },  '<');
$root->set_binding(sub { winflip($cui) },    'w');
$root->set_binding(sub { feedpoll($cui) },   'r');
$root->set_binding(sub { forcefetch($cui) }, 'R');
$root->set_binding(sub { quitit($cui) },     'Q');
$root->set_binding(sub { storyprev($cui) },  '[');
$root->set_binding(sub { storynext($cui) },  ']');
$root->set_binding(sub { helppop($cui) },    'h','?');

# go!
setpollwait($cui);
feedpoll($cui);
$cui->mainloop();

### End
